package com 
{
	import com.greensock.loading.LoaderMax;
	import com.greensock.loading.XMLLoader;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;
	/**
	 * ...
	 * @author Matthew Ladouceur
	 */
	public class SkillList extends FlaObject
	{
		public var _skills:Vector.<Skill> = new Vector.<Skill>();
		
		private var _mask:Sprite = new Sprite();
		private var _content:Sprite = new Sprite();
		private var _scrollBar:ScrollBarLite;
		
		private var _scrollFace:Sprite = new Sprite();
		private var _scrollTrack:Sprite = new Sprite();
		
		private var _listHeight:Number = 0;
		
		private var _spent:Number = 0;
		
		public function get spent():Number {
			_spent = 0;
			for (var i:int = 0; i < _skills.length; i++) {
				_spent += _skills[i].cost;
			}
			return _spent;
		}
		
		public function get skills():XML {
			var xml:XML = <skills/>;
			for (var i:int = 0; i < _skills.length; i++) {
				var skill:XML = <skill/>
				
				var name:XML = <name/>;
				name.appendChild(_skills[i].name);
				var cost:XML = <cost/>;
				cost.appendChild(_skills[i].cost);
				var level:XML = <level/>;
				level.appendChild(_skills[i].level);
				
				skill.appendChild(name);
				skill.appendChild(cost);
				skill.appendChild(level);
				
				xml.appendChild(skill);
			}			
			return xml;
		}
		
		public function SkillList() 
		{
			//_content.graphics.beginFill(0xeeeeeee);
			//_content.graphics.drawRoundRect(0, 0, 300, 2000, 10, 10);
			//_content.graphics.endFill();
			addChild(_content);
			
			_mask.graphics.beginFill(0x000000);
			_mask.graphics.drawRoundRect(0, 0, 300, 400, 10, 10);
			_mask.graphics.endFill();
			addChild(_mask);
			_content.mask = _mask;
			
			_scrollFace.graphics.beginFill(0xff0000);
			_scrollFace.graphics.drawRoundRect(0, 0, 16, 64, 5, 5);
			_scrollFace.graphics.endFill();
			_scrollFace.x = 300;
			
			_scrollTrack.graphics.beginFill(0x0000ff);
			_scrollTrack.graphics.drawRoundRect(0, 0, 16, 400, 5, 5);
			_scrollTrack.graphics.endFill();
			_scrollTrack.x = 300;
			
			addChild(_scrollTrack);
			addChild(_scrollFace);			
						
			_scrollBar = new ScrollBarLite(_scrollFace, _scrollFace, _scrollFace, _content, _mask, _scrollTrack);
			_scrollBar.registerMouseWheelTarget(this);
			_scrollBar.registerCustomContentScroll(_onScroll);
			_scrollBar.onContentSizeChange();
		}
		
		public function update(string:String):void {
			for (var i:int = 0; i < _skills.length; i++) {
				_skills[i].updateCost(string);
			}
		}
		
		public function removeSkill(skill:Skill):void {
			for (var i:int = 0; i < _skills.length; i++) {
				if (skill == _skills[i]) {
					if (_skills[i].level > 1) {
						_skills[i].level--;
					}else{
						_content.removeChild(_skills[i]);
						_skills.splice(i, 1);
					}
					
					_listHeight = 0;
					for (var j:int = 0; j < _skills.length; j++) {
						_skills[j].y = _listHeight;
						_listHeight += 30;
					}
					
					break;
				}
			}
		}
		
		public function addSkill(skill:Skill):void {
			if (skill.name == "Craftsmen" || skill.name == "Sphere of Magic: 1st" || skill.name == "Sphere of Magic: 2nd" || skill.name == "Sphere of Magic: 3rd"
				|| skill.name == "Slay/Parry" || skill.name == "Slay/Parry: Master" || skill.name == "Specialization +1: Group" || skill.name == "Specialization +1: Specific" 
				|| skill.name == "Weapon Proficiency: Exotic" || skill.name == "Weapon Refocus" || skill.name == "Critical +2: Specific" || skill.name == "Critical +2: Group" 
				|| skill.name == "Execute" || skill.name == "Execute: Master" || skill.name == "Create (Sphere) Familiar") {
				return;
			}
			for (var i:int = 0; i < _skills.length; i++) {
				if (_skills[i].name == skill.name) {
					_skills[i].level++;
					return;
				}
			}
			
			var newSkill:Skill = new Skill(skill.data, skill.cost, skill.name);
			newSkill._currentClass = skill._currentClass;
			_content.addChild(newSkill);
			newSkill.y = _listHeight;
			_listHeight += 30;
			_skills.push(newSkill);
			_scrollBar.onContentSizeChange();
		}
		
		public function loadSkill(theCost:Number, theName:String, theLevel:Number, theClass:String):void {
			var newSkill:Skill = new Skill(null, theCost, theName, theLevel);
			newSkill._currentClass = theClass;
			_content.addChild(newSkill);
			newSkill.y = _listHeight;
			_listHeight += 30;
			_skills.push(newSkill);
			_scrollBar.onContentSizeChange();
		}
		
		private function onClick(e:MouseEvent):void {
			dispatchEvent(e);
		}

		private function _onScroll(newY:Number):void{
			var len:uint;
			_content.y = newY;
		} 
		
	}

}